package com.sc.nft.dao;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.entity.vo.MerchantPledgeDataExportPreviewVO;
import com.sc.nft.entity.vo.MerchantUserPledgeTaskPageVO;
import com.sc.nft.entity.vo.UserMerchantPledgeTaskRecordDetailsVO;
import com.sc.nft.entity.vo.UserPledgeTaskRwardsDetailsVO;
import com.sc.nft.entity.vo.merchantPledgeTask.TreasureRecordDetailsVO;
import com.sc.nft.entity.vo.merchantPledgeTask.TreasureRecordPageVO;
import com.sc.nft.enums.UserMerchantPledgeTaskStatusEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.UserMerchantPledgeTaskRecords;
import com.sc.nft.mapper.UserMerchantPledgeTaskRecordsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 用户商户质押任务记录表(sc_user_merchant_pledge_task_records)数据DAO
 *
 * @author Sun
 * @since 2023-08-24 16:16:30
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class UserMerchantPledgeTaskRecordsDao extends ServiceImpl<UserMerchantPledgeTaskRecordsMapper, UserMerchantPledgeTaskRecords> {

    public Page<TreasureRecordPageVO> treasureRecordPage(Long userId, Integer pageNo, Integer pageSize) {
        return baseMapper.treasureRecordPage(new Page<>(pageNo,pageSize),userId);
    }

    public TreasureRecordDetailsVO treasureRecordDetails(Long userId, Long id) {
        return baseMapper.treasureRecordDetails(userId,id);
    }

    public UserMerchantPledgeTaskRecords getByUserIdAndId(Long userId, Long id) {
        LambdaQueryWrapper<UserMerchantPledgeTaskRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserMerchantPledgeTaskRecords::getUserId,userId);
        wrapper.eq(UserMerchantPledgeTaskRecords::getId,id);
        return getOne(wrapper);
    }

    public boolean updateStatusById(Long id, UserMerchantPledgeTaskStatusEnum status) {
        LambdaUpdateWrapper<UserMerchantPledgeTaskRecords> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserMerchantPledgeTaskRecords::getId,id);
        wrapper.set(UserMerchantPledgeTaskRecords::getStatus,status);
        return update(wrapper);
    }

    public List<UserMerchantPledgeTaskRecords> getListByStatus(UserMerchantPledgeTaskStatusEnum status) {
        LambdaQueryWrapper<UserMerchantPledgeTaskRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserMerchantPledgeTaskRecords::getStatus,status);
        return list(wrapper);
    }

    public Integer getSumPledgeCountByUserIdAndMerchantPledgeTaskId(Long userId, Long merchantPledgeTaskId) {
        return baseMapper.getSumPledgeCountByUserIdAndMerchantPledgeTaskId(userId,merchantPledgeTaskId);
    }

    public List<UserMerchantPledgeTaskRecords> getExpireList(UserMerchantPledgeTaskStatusEnum status) {
        return baseMapper.getExpireList(status.getCode());
    }

    public boolean addRewardsCountById(Long id) {
        LambdaUpdateWrapper<UserMerchantPledgeTaskRecords> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserMerchantPledgeTaskRecords::getId,id);
        wrapper.setSql("rewards_count = rewards_count + 1");
        return update(wrapper);
    }

    public Integer getCountByJuniorIdsAndMerchantPledgeTaskId(List<Long> ids, Long merchantPledgeTaskId) {
        return baseMapper.getCountByJuniorIdsAndMerchantPledgeTaskId(merchantPledgeTaskId,ids);
    }

    public Integer getCountMerchantPledgeTaskId( Long merchantPledgeTaskId) {
        return baseMapper.getCountMerchantPledgeTaskId(merchantPledgeTaskId);
    }

    public Page<MerchantUserPledgeTaskPageVO> getMerchantUserPledgeTaskRecordPage(String daoName, String userTel, String pledgeName, UserMerchantPledgeTaskStatusEnum status, Integer pageNo, Integer pageSize,Boolean isFrozen) {
        return baseMapper.getMerchantUserPledgeTaskRecordPage(new Page<>(pageNo, pageSize),daoName, userTel, pledgeName, ObjectUtil.isNull(status)?null:status.getCode(),isFrozen);
    }

    public UserMerchantPledgeTaskRecordDetailsVO userPledgeTaskRecordDetails(Long id) {
        return baseMapper.userPledgeTaskRecordDetails(id);
    }

    public Page<UserPledgeTaskRwardsDetailsVO> getMerchantUserPledgeTaskRwardsDetails(Page page, Long id, Date startTime, Date endTime) {
        return baseMapper.getMerchantUserPledgeTaskRwardsDetails(page,id,startTime,endTime);
    }

    public List<MerchantPledgeDataExportPreviewVO> merchantPledgeDataExportPreview(String pledgeName, Long daoId) {
        return baseMapper.merchantPledgeDataExportPreview(pledgeName,daoId);
    }

    public Integer getSumPledgeCountByUserIdAndMerchantPledgeTaskIdAndStatus(Long userId, Long id) {
        return baseMapper.getSumPledgeCountByUserIdAndMerchantPledgeTaskIdAndStatus(userId,id);
    }
}